class Solution {
public:
    int happysum(int a)
    {
        int count=0;
        while(a)
        {
            int b=a%10;
            count+=b*b;
            a/=10;
        }
        return count;
    }

    bool isHappy(int n) {
        int slow=n;
        int fast=n;
        fast=happysum(fast);
        while(slow!=fast)
        {
            slow=happysum(slow);
            fast=happysum(fast);
            fast=happysum(fast);
        }
        return fast==1;
    }
};
